package com.lhb.service.serviceImpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lhb.constant.MessageConstant;
import com.lhb.constant.StatusConstant;
import com.lhb.context.BaseContext;
import com.lhb.dto.UserAddDTO;
import com.lhb.dto.UserEditDTO;
import com.lhb.dto.UserLoginDTO;
import com.lhb.dto.UserPageQueryDTO;
import com.lhb.entity.User;
import com.lhb.exception.AccountLockedException;
import com.lhb.exception.AccountNotFoundException;
import com.lhb.exception.PasswordErrorException;
import com.lhb.mapper.UserMapper;
import com.lhb.result.PageResult;
import com.lhb.service.UserService;
import com.lhb.vo.UserEditVO;
import com.lhb.vo.UserGetByIdVO;
import com.lhb.vo.UserPageQueryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    /**
     * 用户登录
     * @param userLoginDTO
     * @return
     */
    public User login(UserLoginDTO userLoginDTO) {

      User user= userMapper.getByUsername(userLoginDTO.getUsername());

        if(user==null){

            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);

        }

        if(!user.getPassword().equals(userLoginDTO.getPassword())){
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (user.getStatus()== StatusConstant.DISABLED)
        {
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }




        return user;
    }

    /**
     * 分页查询
     * @param userPageQueryDTO
     * @return
     */
    public PageResult pageQuery(UserPageQueryDTO userPageQueryDTO) {

        PageHelper.startPage(userPageQueryDTO.getCurrentPage(),userPageQueryDTO.getPageSize());

        Page<UserPageQueryVO> page= userMapper.pageQuery(userPageQueryDTO);


        Long total=page.getTotal();
        List<UserPageQueryVO> userPageQueryVOList=page.getResult();


        return new PageResult(total,userPageQueryVOList);
    }

    /**
     * 根据id查询用户
     *
     * @param id
     * @return
     */
    public UserGetByIdVO getById(Integer id) {


        return userMapper.getById(id);
    }

    /**
     * 编辑用户
     * @param userEditDTO
     * @return
     */
    public void update(UserEditDTO userEditDTO) {

        User user = new User();
        BeanUtils.copyProperties(userEditDTO, user);
        user.setUpdateUser(BaseContext.getCurrentId());
        user.setUpdateTime(LocalDateTime.now());

        userMapper.update(user);
    }

    /**
     * 修改用户状态
     * @param status
     * @param id
     */
    public void modifyStatus(Integer status, Long id) {

        User user = User.builder()
                .id(id)
                .status(status)
                .updateUser(BaseContext.getCurrentId())
                .updateTime(LocalDateTime.now())
                .build();


        userMapper.modifyStatus(user);


    }

    /**
     * 根据id删除用户
     * @param id
     */
    public void deleteById(Integer id) {

        userMapper.deleteById(id);

    }

    /**
     * 添加用户
     *
     * @param userAddDTO
     */
    public void save(UserAddDTO userAddDTO) {
        User user = new User();
        BeanUtils.copyProperties(userAddDTO,user);
        user.setStatus(StatusConstant.ENABLED);
        user.setCreateUser(BaseContext.getCurrentId());
        user.setUpdateUser(BaseContext.getCurrentId());
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());

        userMapper.save(user);

    }
}
